package top.infopub.mgr.bpa.service.impl;


import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.InnerSettleAttDao;
import top.infopub.mgr.bpa.dao.InnerSettleDao;
import top.infopub.mgr.bpa.dao.InnerSettleDetailDao;
import top.infopub.mgr.bpa.dao.InnerSettleListDao;
import top.infopub.mgr.bpa.dao.InnerSettleSubDao;
import top.infopub.mgr.bpa.domain.InnerSettleDomain;
import top.infopub.mgr.bpa.service.InnerSettleService;
import top.infopub.mgr.bpa.vo.InnerSettleVo;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.model.bpa.InnerSettle;
import top.infopub.model.bpa.InnerSettleAtt;
import top.infopub.model.bpa.InnerSettleDetail;
import top.infopub.model.bpa.InnerSettleList;
import top.infopub.model.bpa.InnerSettleSub;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class InnerSettleServiceImpl extends GenericServiceImpl<InnerSettle, String> implements InnerSettleService {

    @Autowired
    private InnerSettleDao innerSettleDao;

    @Autowired
    private InnerSettleSubDao innerSettleSubDao;

    @Autowired
    private InnerSettleDetailDao innerSettleDetailDao;

    @Autowired
    private InnerSettleAttDao innerSettleAttDao;

    @Autowired
    private InnerSettleListDao innerSettleListDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<InnerSettle, String> getDao() {
        return innerSettleDao;
    }

    @Override
    public Page<InnerSettleDomain> selectByDomainWhere(Page<InnerSettleDomain> page,
                                                       InnerSettleVo innerSettleVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<InnerSettleDomain>)innerSettleDao.selectByDomainWhere(innerSettleVo);
    }

    @Override
    public Page<InnerSettleDomain> selectMyCheckByDomainWhere(Page<InnerSettleDomain> page,
                                                              InnerSettleVo innerSettleVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<InnerSettleDomain>)innerSettleDao.selectMyCheckByDomainWhere(innerSettleVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInnerSettle(InnerSettle innerSettle, List<InnerSettleList> innerSettleLists,
                                List<InnerSettleSub> innerSettleSubs,
                                List<InnerSettleDetail> innerSettleDetails,
                                List<InnerSettleAtt> innerSettleAtts) {
        //主表新增
        innerSettleDao.insertById(innerSettle);

        if (CollectionUtils.isNotEmpty(innerSettleLists)) {
            innerSettleListDao.insertByBatch(innerSettleLists);
        }

        if (CollectionUtils.isNotEmpty(innerSettleSubs)) {
            innerSettleSubDao.insertByBatch(innerSettleSubs);
        }

        if (CollectionUtils.isNotEmpty(innerSettleDetails)) {
            innerSettleDetailDao.insertByBatch(innerSettleDetails);
        }

        if (CollectionUtils.isNotEmpty(innerSettleAtts)) {
            innerSettleAttDao.insertByBatch(innerSettleAtts);
        }

        // 50万
        BigDecimal cmp = new BigDecimal("500000");
        String srcBizTable = "BPA_INNER_SETTLE";
        String branchCode_lt = "total_sum_inner<=50万";
        String branchCode_gt = "total_sum_inner>50万";
        String templateId = "";
        // <=50万
        if (innerSettle.getTotalSumInner().compareTo(cmp) <= 0) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_lt);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_gt);
        }
        // 处理工作流数据
        wsFlowProcess.launch(templateId, srcBizTable, innerSettle.getId(),
            innerSettle.getProcessId(), null);
    }

    @Override
    public InnerSettleVo queryById(String id) {
        return innerSettleDao.queryById(id);
    }

}